package com.zycfc.zsf.boot.mybatis.service;

import com.zycfc.zsf.boot.data.domain.*;
import java.io.*;
import com.zycfc.zsf.boot.data.service.*;
import ch.qos.logback.classic.*;
import com.zycfc.zsf.boot.mybatis.mapper.*;
import com.zycfc.zsf.boot.util.bean.*;
import com.zycfc.zsf.boot.util.text.*;
import org.springframework.transaction.annotation.*;
import org.springframework.util.*;
import com.zycfc.zsf.boot.util.time.*;
import java.util.*;
import com.github.pagehelper.*;
import org.slf4j.*;

public interface AbstractService<T extends BaseDomain, Pk extends Serializable> extends GenericService<T, Pk>
{
    public static final Logger log = (Logger)LoggerFactory.getLogger((Class)AbstractService.class);
    public static final int PAGENUM = 1;
    public static final int PAGESIZE_20 = 20;
    public static final int PAGESIZE_50 = 50;
    public static final int PAGESIZE_100 = 100;
    
    GenericMapper<T, Pk> getMapper();
    
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
    default void add(final T entity) {
        if (entity.getId() == null || Objects.isEquals((Object)entity.getId(), (Object)"")) {
            entity.setId((Serializable)UuidUtils.getUuid());
        }
        this.setCreateAndUpdateTime(entity);
        this.getMapper().add(entity);
    }
    
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
    default void addBatch(final List<T> list) {
        if (StringUtils.isEmpty((Object)list) || list.size() == 0) {
            return;
        }
        final List<T> subList = new ArrayList<T>();
        int i = 1;
        int k = 1;
        for (final T entity : list) {
            if (entity.getId() == null || Objects.isEquals((Object)entity.getId(), (Object)"")) {
                entity.setId((Serializable)UuidUtils.getUuid());
            }
            this.setCreateAndUpdateTime(entity);
            subList.add(entity);
            if (i % 500 == 0) {
                final long t1 = System.currentTimeMillis();
                this.getMapper().addBatch(subList);
                AbstractService.log.info("\u6267\u884c\u5e8f\u53f7\uff1a" + k + ",\u6267\u884c\u65f6\u95f4:" + (System.currentTimeMillis() - t1) + ",\u7c7b\u540d:" + entity.getClass().getName());
                ++k;
                subList.clear();
            }
            ++i;
        }
        if (subList.size() > 0) {
            final long t2 = System.currentTimeMillis();
            this.getMapper().addBatch(subList);
            AbstractService.log.debug("\u6267\u884c\u5e8f\u53f7\uff1a" + k + ",\u6267\u884c\u65f6\u95f4:" + (System.currentTimeMillis() - t2));
            subList.clear();
        }
    }
    
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
    default int updateById(final T entity) {
        entity.setUpdateTime((Date)DateUtils.getCurTimestamp());
        return this.getMapper().updateById(entity);
    }
    
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
    default int deleteById(final Pk pk) {
        return this.getMapper().deleteById(pk);
    }
    
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
    default int delLogicById(final Pk pk) {
        return this.getMapper().delLogicById(pk);
    }
    
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
    default int deleteBatchIds(final List<? extends Pk> pkList) {
        return this.getMapper().deleteBatchIds(pkList);
    }
    
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = { Exception.class }, readOnly = true)
    default List<T> findAll() {
        return this.getMapper().findAll();
    }
    
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = { Exception.class }, readOnly = true)
    default T findOneById(final Pk pk) {
        return this.getMapper().findOneById(pk);
    }
    
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = { Exception.class }, readOnly = true)
    default List<T> find(final T condition) {
        return this.getMapper().find(condition);
    }
    
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = { Exception.class }, readOnly = true)
    default List<T> findByPage(final int pageNum, final int pageSize, final T condition) {
        PageHelper.startPage(pageNum, pageSize);
        return this.getMapper().find(condition);
    }
    
    default void setCreateAndUpdateTime(final T entity) {
        if (entity.getCreateTime() == null) {
            entity.setCreateTime((Date)DateUtils.getCurTimestamp());
        }
        if (entity.getUpdateTime() == null) {
            entity.setUpdateTime((Date)DateUtils.getCurTimestamp());
        }
    }
}
